﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class QL_GopY : System.Web.UI.Page
{
    private string Connect = ConfigurationManager.ConnectionStrings["ketnoi"].ConnectionString;
    SqlConnection con;
    SqlDataAdapter ad;
    DataTable dt = new DataTable();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"].ToString() == "")
        {
            Response.Redirect("Trang_Chu.aspx");
        }
        if (!IsPostBack)
        {
            hienthiGri();
        }
        
        hienthiDatal();
       
       
    }
    public void hienthiDatal()
    {
        string sql = "select top 7 * from gop_y where ngay_gui >=(getdate()-5) order by ngay_gui desc";
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            DataList1.DataSource = null;
            dt.Clear();
            ad = new SqlDataAdapter(sql, con);
            ad.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                DataList1.DataSource = dt;
                DataList1.DataBind();
                lblgopy.Text = "Có " + dt.Rows.Count + " phản hồi mới từ khách hàng";
            }
            else
            {
                lblgopy.Text = "Không có phản hồi mới nào từ khách hàng";
            }
        }
        catch(Exception ex)
        {
            lbl.Text = "Lỗi: " + ex.Message;
        }        
        con.Dispose();
        ad.Dispose();
        DataList1.Dispose();
        con.Close();
    }
    public void hienthiGri()
    {
        string sql = "select * from GOP_Y";
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            GridView1.DataSource = null;
            dt.Clear();
            ad = new SqlDataAdapter(sql, con);
            ad.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            else
            {
                lbl.Text = "Không có góp ý nào";
            }
        }
        catch (Exception ex)
        {
            lbl.Text = "Lỗi:" + ex.Message;
        }
    }
    protected void btnxoa_Click(object sender, EventArgs e)
    {
        lbl.Text = "";
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Delete from gop_y where idgopy=" + txtma.Text;
            int i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                lbl.Text = "Đã xóa xong 1 mãu tin";
                hienthiGri();
                Page_Load(sender, e);
                txtma.Text = "";
            }
            else
            {
                lbl.Text = "Không xóa được";
            }
        }
        catch (Exception ex)
        {
            lbl.Text = "Lỗi: "+ex.Message;
        }

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int idrow = GridView1.SelectedIndex;
        txtma.Text = GridView1.Rows[idrow].Cells[0].Text.ToString();

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
    }
}
